package com.suwish.pc.ui.preference;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.suwish.pc.ui.component.UIDialog;
import com.suwish.pc.ui.component.list.PreferenceListCellRenderer;
import com.suwish.pc.ui.util.UIHelper;
/**
 * 
 * @author Min
 *
 */
public class PreferenceDialog extends UIDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JList elementList = null;
	private DefaultListModel listModel = null;
	
	private JPanel contentPanel = null;
	private Preference currentPreference = null;
	
	public PreferenceDialog(Window parent){
		super(parent, "Preferences");
		setModal(true);
		initUI();
		intEvent();
		setSize(new Dimension(600, 600));
		setLocationRelativeTo(parent);
	}
	private void intEvent(){
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				dispose();
			}			
		});
		elementList.addListSelectionListener(new ListSelectionListener() {			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting() || elementList.getSelectedIndex() == -1)return;
				PreferenceUI ui = (PreferenceUI)elementList.getSelectedValue();
				currentPreference = ui.getPreference();
				currentPreference.load();
				
				contentPanel.removeAll();
				contentPanel.add(currentPreference.getComponent(), BorderLayout.CENTER);
				
				contentPanel.invalidate();
				contentPanel.validate();
				contentPanel.repaint();
			}
		});
	}
	private void initUI(){
		JPanel mainPanel = getIContentPane();
		mainPanel.setBackground(Color.WHITE);
		mainPanel.setLayout(new BorderLayout());
		elementList = new JList(listModel = new DefaultListModel());
		elementList.setCellRenderer(new PreferenceListCellRenderer());
		
		JScrollPane scrollPanel = new JScrollPane(elementList);
		scrollPanel.setBorder(BorderFactory.createEmptyBorder());
		scrollPanel.setPreferredSize(new Dimension(100, scrollPanel.getHeight()));
		mainPanel.add(scrollPanel, BorderLayout.WEST);
		
		contentPanel = new JPanel(new BorderLayout());
		mainPanel.add(contentPanel, BorderLayout.CENTER);
		
		JPanel buttomPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
		mainPanel.add(buttomPane, BorderLayout.SOUTH);
//		buttomPane.setOpaque(false);
		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				if(elementList == null || currentPreference == null) return;				
				try{
					currentPreference.save();
					dispose();
				}catch(Exception ex){
					UIHelper.showErrorDialog(getInstance(), ex.getMessage());
				}
			}
		});
		JButton applyButton = new JButton("Apply");
		applyButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				if(elementList == null || currentPreference == null) return;				
				try{
					currentPreference.save();
				}catch(Exception ex){
					UIHelper.showErrorDialog(getInstance(), ex.getMessage());
				}
			}
		});
		JButton cancelButton = new JButton("Cancel");
		buttomPane.add(submitButton);
		buttomPane.add(applyButton);
		buttomPane.add(cancelButton);
		cancelButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();				
			}
		});
	}
	
	public void initData(List<Preference> preferences){
		if(preferences == null || preferences.size() == 0) return;
		listModel.removeAllElements();
		for(Preference preference : preferences){
			listModel.addElement(new PreferenceUI(preference));
		}
		elementList.setSelectedIndex(0);
	}
}
